WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
utils_gtest.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include <gtest/gtest.h>
22#include <Eigen/Dense>
23
24// Macros for testing equalities and inequalities.
25//
26// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
27// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2
28// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2
29// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2
30// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2
31// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2
32//
33// C String Comparisons. All tests treat NULL and any non-NULL string
34// as different. Two NULLs are equal.
35//
36// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2
37// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2
38// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
39// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
40//
41// Macros for comparing floating-point numbers.
42//
43// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
44// Tests that two float values are almost equal.
45// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
46// Tests that two double values are almost equal.
47// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
48// Tests that v1 and v2 are within the given distance to each other.
49//
50// These predicate format functions work on floating-point values, and
51// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
52//
53// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
54//
55// Macros that execute statement and check that it doesn't generate new fatal
56// failures in the current thread.
57//
58// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
59
65{
66 // gtest/CMakeLists.txt defines this environment variable when launching tests
67 return std::getenv("DASHBOARD_TEST_FROM_CTEST") != NULL;
68}
69
70namespace testing
71{
72/* Macros related to testing Eigen classes:
73 */
74#define EXPECT_MATRIX_APPROX(C_expect, C_actual, precision) \
75 EXPECT_PRED2([](const Eigen::MatrixXd& lhs, \
76 const Eigen::MatrixXd& rhs) { return (lhs - rhs).isMuchSmallerThan(1, precision); }, \
77 C_expect, \
78 C_actual);
79
80#define ASSERT_MATRIX_APPROX(C_expect, C_actual, precision) \
81 ASSERT_PRED2([](const Eigen::MatrixXd& lhs, \
82 const Eigen::MatrixXd& rhs) { return (lhs - rhs).isMuchSmallerThan(1, precision); }, \
83 C_expect, \
84 C_actual);
85
86#define EXPECT_QUATERNION_APPROX(C_expect, C_actual, precision) \
87 EXPECT_PRED2([](const Eigen::Quaterniond& lhs, \
88 const Eigen::Quaterniond& rhs) { return lhs.angularDistance(rhs) < precision; }, \
89 C_expect, \
90 C_actual);
91
92#define ASSERT_QUATERNION_APPROX(C_expect, C_actual, precision) \
93 ASSERT_PRED2([](const Eigen::Quaterniond& lhs, \
94 const Eigen::Quaterniond& rhs) { return lhs.angularDistance(rhs) < precision; }, \
95 C_expect, \
96 C_actual);
97
98#define EXPECT_QUATERNION_VECTOR_APPROX(C_expect, C_actual, precision) \
99 EXPECT_PRED2( \
100 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
101 return Eigen::Quaterniond(Eigen::Vector4d(lhs)) \
102 .angularDistance(Eigen::Quaterniond(Eigen::Vector4d(rhs))) < precision; \
103 }, \
104 C_expect, \
105 C_actual);
106
107#define ASSERT_QUATERNION_VECTOR_APPROX(C_expect, C_actual, precision) \
108 ASSERT_PRED2( \
109 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
110 return Eigen::Quaterniond(Eigen::Vector4d(lhs)) \
111 .angularDistance(Eigen::Quaterniond(Eigen::Vector4d(rhs))) < precision; \
112 }, \
113 C_expect, \
114 C_actual);
115
116#define EXPECT_POSE2d_APPROX(C_expect, C_actual, precision) \
117 EXPECT_PRED2( \
118 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
119 assert(lhs.size() == 3 and rhs.size() == 3); \
120 Eigen::VectorXd er = lhs - rhs; \
121 er(2) = remainder((double)er(2), 2 * M_PI); \
122 return er.isMuchSmallerThan(1, precision); \
123 }, \
124 C_expect, \
125 C_actual);
126
127#define ASSERT_POSE2d_APPROX(C_expect, C_actual, precision) \
128 ASSERT_PRED2( \
129 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
130 assert(lhs.size() == 3 and rhs.size() == 3); \
131 Eigen::VectorXd er = lhs - rhs; \
132 er(2) = remainder((double)er(2), 2 * M_PI); \
133 return er.isMuchSmallerThan(1, precision); \
134 }, \
135 C_expect, \
136 C_actual);
137
138#define EXPECT_POSE3d_APPROX(C_expect, C_actual, precision) \
139 EXPECT_PRED2( \
140 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
141 assert(lhs.size() == 7 and rhs.size() == 7); \
142 Eigen::Vector4d er; \
143 er.head(3) = lhs.head(3) - rhs.head(3); \
144 er(3) = Eigen::Quaterniond(Eigen::Vector4d(lhs.tail(4))) \
145 .angularDistance(Eigen::Quaterniond(Eigen::Vector4d(rhs.tail(4)))); \
146 return er.isMuchSmallerThan(1, precision); \
147 }, \
148 C_expect, \
149 C_actual);
150
151#define ASSERT_POSE3d_APPROX(C_expect, C_actual, precision) \
152 ASSERT_PRED2( \
153 [](const Eigen::VectorXd& lhs, const Eigen::VectorXd& rhs) { \
154 assert(lhs.size() == 7 and rhs.size() == 7); \
155 Eigen::Vector4d er; \
156 er.head(3) = lhs.head(3) - rhs.head(3); \
157 er(3) = Eigen::Quaterniond(Eigen::Vector4d(lhs.tail(4))) \
158 .angularDistance(Eigen::Quaterniond(Eigen::Vector4d(rhs.tail(4)))); \
159 return er.isMuchSmallerThan(1, precision); \
160 }, \
161 C_expect, \
162 C_actual);
163
164} // namespace testing
Definition: utils_gtest.h:71
bool is_running_under_ctest()
Returns whether the code is running under CTest.
Definition: utils_gtest.h:64